package com.software.loginregister;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.software.loginregister.activity.PublishActivity;
import com.software.loginregister.adapter.ViewPagerAdapter;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 vpMain;
    private BottomNavigationView btnNav;
    private FloatingActionButton fabAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vpMain = findViewById(R.id.vp_main);
        btnNav = findViewById(R.id.btn_nav);
        fabAdd = findViewById(R.id.fab_add);
        fabAdd.setOnClickListener(v -> {
            // 跳转到发布作品界面
            Intent intent = new Intent(MainActivity.this, PublishActivity.class);
            startActivity(intent);
        });


        // 设置ViewPager适配器
        vpMain.setAdapter(new ViewPagerAdapter(this));

        // 禁用ViewPager的滑动
        vpMain.setUserInputEnabled(false);

        // 设置底部导航点击监听
        btnNav.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                vpMain.setCurrentItem(0);
            } else if (itemId == R.id.nav_ai) {
                vpMain.setCurrentItem(1);
            } else if (itemId == R.id.nav_favorite) {
                vpMain.setCurrentItem(2);
            } else if (itemId == R.id.nav_profile) {
                vpMain.setCurrentItem(3);
            }
            return true;
        });


        // 添加布局变化监听器
        final View rootView = findViewById(android.R.id.content);
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = rootView.getRootView().getHeight() - rootView.getHeight();
                if (heightDiff > dpToPx(200)) { // 如果高度差大于200dp，认为键盘显示
                    hideNavigationBar();
                } else {
                    showNavigationBar();
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }



    public void hideNavigationBar() {
        WindowInsetsControllerCompat insetsController = new WindowInsetsControllerCompat(
                getWindow(), getWindow().getDecorView());
        insetsController.hide(WindowInsetsCompat.Type.navigationBars());
    }

    public void showNavigationBar() {
        WindowInsetsControllerCompat insetsController = new WindowInsetsControllerCompat(
                getWindow(), getWindow().getDecorView());
        insetsController.show(WindowInsetsCompat.Type.navigationBars());
    }

    private int dpToPx(int dp) {
        float density = getResources().getDisplayMetrics().density;
        return Math.round(dp * density);
    }
}